//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
package org.jpgolay.gui.progressBar;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

// Referenced classes of package org.jpgolay.gui.progressBar:
//            LongTask
public class ProgressBar extends JDialog
        implements ActionListener {

    public ProgressBar() {
        newline = "\n";
        stop = true;
        init();
    }

    public ProgressBar(Frame parent, boolean modal) {
        super(parent, modal);
        newline = "\n";
        stop = true;
        init();
    }

    private void init() {
        setTitle("Operation en cours...");
        task = new LongTask();
        startButton = new JButton("Annuler");
        startButton.setToolTipText("Merci de patienter, cette op\351ration peu prendre quelques minutes selon votre connexion r\351seau.");
        startButton.setActionCommand("start");
        startButton.addActionListener(this);
        progressBar = new JProgressBar(0, task.getLengthOfTask());
        progressBar.setValue(0);
        progressBar.setStringPainted(true);
        progressBar.setString("");
        JPanel panel = new JPanel();
        panel.setOpaque(true);
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        panel.setLayout(new GridLayout(2, 1));
        panel.add(progressBar);
        panel.add(startButton);
        getContentPane().add(panel, "First");
        timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                progressBar.setValue(task.getCurrent());
                if (task.getMessage() != null && progressBar.isIndeterminate()) {
                    progressBar.setIndeterminate(false);
                    progressBar.setString(null);
                }
                if (task.isDone()) {
                    stop();
                }
            }
        });
        setSize(300, 100);
    }

    public void actionPerformed(ActionEvent evt) {
        stop();
    }

    public void start() {
        stop = false;
        progressBar.setIndeterminate(true);
    }

    public void stop() {
        Toolkit.getDefaultToolkit().beep();
        progressBar.setValue(progressBar.getMinimum());
        progressBar.setString("");
        stop = true;
    }

    public boolean isStop() {
        return stop;
    }
    public static final int ONE_SECOND = 1000;
    private JProgressBar progressBar;
    private Timer timer;
    private JButton startButton;
    private LongTask task;
    private String newline;
    private boolean stop;
}
